﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Xml.Serialization;
using TV_Organizer.Properties;
using System.Reflection;

namespace TV_Organizer
{
    /// <summary>
    /// Logique d'interaction pour App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static App Instance { get; private set; }

        public System.Windows.Forms.NotifyIcon NotificationIcon { get; private set; }

        public MainWindow Window { get; private set; }

        protected override void OnStartup(StartupEventArgs e)
        {
            #region Instance

            if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Count() > 1)
            {
                MessageBox.Show("An instance of this application is already running. Only one instance can be runned at a time.",
                    "TV Organizer is already running", MessageBoxButton.OK, MessageBoxImage.Information);
                this.Shutdown(0);
            }

            Instance = this;

            #endregion

            #region traces init

            var traceListeners = new TextWriterTraceListener[]
            {
                new TextWriterTraceListener(Constants.LogFile),
            };
            Debug.Listeners.AddRange(traceListeners);
            LogsManager.Log(this, "APPLICATION STARTED");

            #endregion

            #region MainWindow + Notification icon init

            this.Window = new MainWindow();

            this.NotificationIcon = new System.Windows.Forms.NotifyIcon
            {
                Icon = new System.Drawing.Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("TV_Organizer.Main.ico")),
                Visible = true,
                ContextMenu = new System.Windows.Forms.ContextMenu
                {
                    MenuItems =
                    {
                        new System.Windows.Forms.MenuItem("Open window", (o,e2) => this.Window.Show()),
                        new System.Windows.Forms.MenuItem("Close", (o,e2) => this.Window.Close()),
                    }
                }
            };
            this.NotificationIcon.DoubleClick += (o, e2) => this.Window.Show();
            this.NotificationIcon.BalloonTipClicked += (o, e2) => this.Window.Show();
            
            if (Settings.Default.StartMinimized)
            {
                this.Notify("TV Organizer started (minimized)");
            }
            else
            {
                this.Window.Show();
            }

            #endregion

            base.OnStartup(e);
        }

        [XmlInclude(typeof(TV_Organizer.EPGuides.EPGuidesShow))]
        protected override void OnExit(ExitEventArgs e)
        {
            this.NotificationIcon.Visible = false;

            Settings.Default.Save();

            foreach (var listener in Debug.Listeners.OfType<TextWriterTraceListener>())
            {
                listener.Flush();
                listener.Close();
            }

            LogsManager.Log(this, "APPLICATION ENDED");

            base.OnExit(e);
        }

        /// <summary>
        /// Opens a balloon on the notification icon.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="icon"></param>
        public void Notify(string text, System.Windows.Forms.ToolTipIcon icon = System.Windows.Forms.ToolTipIcon.None)
        {
            if (Settings.Default.DisableNotifications) return;

            this.NotificationIcon.ShowBalloonTip(5000, "TV Organizer", text, icon);
        }
    }
}
